<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	
	
	switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];
    	    $start		=	$_REQUEST['start'];
    	    $end		=	$_REQUEST['end'];
    	    $rResult = mysql_query("SELECT       sale_transaction_detail.id        AS id,
									             production.name                  AS product,
									             ROUND(SUM(sale_transaction_detail.product_quantity  *  sale_transaction_detail.cost), 2) AS sumCost,
									             ROUND(SUM(sale_transaction_detail.product_quantity  * sale_transaction_detail.self_cost),2) AS `self_cost`,
									             ROUND(SUM(sale_transaction_detail.product_quantity  *  sale_transaction_detail.cost) - SUM(sale_transaction_detail.product_quantity  * sale_transaction_detail.self_cost),2) AS `profit`,
									             CONCAT(ROUND((SUM(sale_transaction_detail.product_quantity  *  sale_transaction_detail.cost) - SUM(sale_transaction_detail.product_quantity  * sale_transaction_detail.self_cost))/(SUM(sale_transaction_detail.product_quantity  * sale_transaction_detail.self_cost))*100,2), ' ', '%')
									FROM         sale_transaction LEFT JOIN sale_transaction_detail
									ON           sale_transaction.id = sale_transaction_detail.transaction_id LEFT JOIN production
									ON           sale_transaction_detail.production_id = production.id
									WHERE        sale_transaction.end_date >= '$start' AND sale_transaction.end_date <= '$end' AND sale_transaction.`status` = 0
									GROUP BY     sale_transaction_detail.production_id");
			
			$output = array(
				"aaData"	=> array()
			);
			
			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}
			
			echo json_encode( $output );

	        break;	    
	    default:
	       	echo "null";
	}
?>